{layout name="common/layout" /}
<section class="panel">
    <header class="panel-heading">
        添加客服
    </header>
    <div class="panel-body">
        {php} if(!empty($group)){ {/php}
        <form class="form-horizontal" id="member_form">
            <div class="form-group">
                <label  class="col-lg-2 col-sm-2 control-label">客服昵称</label>
                <div class="col-lg-10">
                    <input type="text" name="seller_name" class="form-control" value="" >
                    <p class="help-block">如:拼图云商城客服</p>
                </div>
            </div>
            <div class="form-group">
                <label  class="col-lg-2 col-sm-2 control-label">客服帐号(手机号)</label>
                <div class="col-lg-10">
                    <input type="tel" name="seller_tel" class="form-control" value="" >
                    <p class="help-block">如:185***1235</p>
                </div>
            </div>
            <div class="form-group">
                <label  class="col-lg-2 col-sm-2 control-label">登录密码</label>
                <div class="col-lg-10">
                    <input type="password" name="seller_pass" class="form-control" value="" >
                    <p class="help-block">如:123456</p>
                </div>
            </div>
            <div class="form-group">
                <label  class="col-lg-2 col-sm-2 control-label">重复密码</label>
                <div class="col-lg-10">
                    <input type="password" name="repeat_pass" class="form-control" value="" >
                    <p class="help-block">如:123456</p>
                </div>
            </div>
            <div class="form-group">
                <label  class="col-lg-2 col-sm-2 control-label">客服真实姓名</label>
                <div class="col-lg-10">
                    <input type="text" name="seller_truename" class="form-control" value="" >
                    <p class="help-block">如:王**</p>
                </div>
            </div>
            <div class="form-group">
                <label  class="col-lg-2 col-sm-2 control-label">用户头像</label>
                {:tplSingleImage('headimg')}
                <div class="row"></div>
                <div class="col-lg-5 col-lg-offset-2">建议尺寸(70*70)</div>
            </div>
            <div class="form-group">
                <label  class="col-lg-2 col-sm-2 control-label">用户权限组</label>
                <div class="col-lg-6">
                    <select class="form-control" id="group">
                        <option value="0">请选择</option>
                        {volist name='group' id='g'}
                        <option value="{$g['id']}">{$g['title']}</option>
                        {/volist}
                    </select>
                </div>
                <div class="col-lg-4"><a class="btn btn-primary" onclick="addgroup();">添加</a></div>
            </div>
            <div class="form-group">
                <label  class="col-lg-2 col-sm-2 control-label"></label>
                <div class="col-lg-10 ">
                    <div class="row"  id="group_show">

                    </div>
                </div>
            </div>
            <div class="form-group">
                <div class="col-lg-offset-2 col-lg-10">
                    <button type="submit" class="btn btn-primary">保存</button>
                </div>
            </div>
        </form>
        {php} }else{ {/php}
        <div class="row text-center">暂无权限组，请先去<a href="{:WebUrl('admincustom/addgroup')}">添加权限组</a></div>
        {php} } {/php}
    </div>

</section>
<script type="text/javascript">
    function addgroup() {
        var group = $('#group').val();
        if(group==0){
            toastTop('请选择要添加的权限组',2);
            return false;
        }
        if($('#group_'+group).length>0){
            toastTop('已添加此权限组',2);
            return false;
        }
        var groupname = $('#group option:selected').text();

        var div_dom = '<div class="col-lg-2" id="group_'+group+'"><a class="btn btn-success">'+groupname+' <span class="badge" onclick="delGroup(this)"><i class="fa fa-times"></i></span></a>'+
                '<input type="hidden" name="group[]" value="'+group+'"><input type="hidden" name="groupname[]" value="'+groupname+'"></div>';

        $('#group_show').append(div_dom);
        return false;
    }
    function delGroup(obj) {
        $(obj).parent().parent().remove();
    }
    $('#member_form').submit(function () {
        var seller_name = $('input[name="seller_name"]').val();
        if(!seller_name){
            toastTop('用户昵称不能为空',2);
            return false;
        }
        var seller_truename = $('input[name="seller_truename"]').val();
        if(!seller_truename){
            toastTop('用户真实姓名不能为空',2);
            return false;
        }
        var headimg = $('input[name="headimg"]').val();
        if(!headimg){
            toastTop('用户头像不能为空',2);
            return false;
        }
        var tel = $('input[name="seller_tel"]').val();
        if(!isPhone(tel)){
            toastTop('手机格式不正确',2);
            return false;
        }
        var pass = $('input[name="seller_pass"]').val();
        if(!pass){
            toastTop('用户密码不能为空',2);
            return false;
        }else{
            var repeat_pass = $('input[name="repeat_pass"]').val();
            if(repeat_pass!=pass){
                toastTop('两次密码不一致',2);
                return false;
            }
        }
        if($('input[name="group[]"]').length<=0){
            toastTop('必须添加至少一个用户权限组',2);
            return false;
        }

        $.ajax({
                    cache: true,
                    type: "POST",
                    url:'{:WebUrl('admincustom/add')}',
                data:$('#member_form').serialize(),// 你的formid
                async: false,
                error: function(data) {
            toastTop('服务器错误，稍后再试',2);
        },
        success: function(data) {
            if(data.status=1){
                toastTop('添加成功');
                setTimeout(function(){
                    window.location='{:WebUrl('admincustom/index')}';
                },600)
            }else{
                toastTop('添加失败');
            }
        }
    },'json');
        return false;
    });


</script>
